import aiohttp

from api.xbot.xbot_base_api import XbotBaseApi


class MsgApi(XbotBaseApi):

    # ==================== Msg 消息模块 ====================
    def _get_api_url(self, endpoint):
        """处理API URL，考虑base_url可能已包含/api的情况"""
        base_url = self.base_url
        if base_url.endswith('/api'):
            return f"{base_url.rstrip('/')}/{endpoint}"
        else:
            return f"{base_url}/{endpoint}"
    
    async def msg_revoke(self, wxid: str, to_user_name: str, client_msg_id: str, create_time: int, new_msg_id: int):
        """撤回消息"""
        body = {
            "Wxid": wxid,
            "ToUserName": to_user_name,
            "ClientMsgId": client_msg_id,
            "CreateTime": create_time,
            "NewMsgId": new_msg_id
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    self._get_api_url("Msg/Revoke"),
                    json=body
            ) as response:
                return await response.json()

    async def msg_send_app(self, wxid: str, to_wxid: str, app_type: int, xml: str):
        """发送App消息"""
        body = {
            "Wxid": wxid,
            "ToWxid": to_wxid,
            "Type": app_type,
            "Xml": xml
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    self._get_api_url("Msg/SendApp"),
                    json=body
            ) as response:
                return await response.json()

    async def msg_send_cdn_file(self, wxid: str, to_wxid: str, content: str):
        """发送文件(转发)"""
        body = {
            "Wxid": wxid,
            "ToWxid": to_wxid,
            "Content": content
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    self._get_api_url("Msg/SendCDNFile"),
                    json=body
            ) as response:
                return await response.json()

    async def msg_send_cdn_img(self, wxid: str, to_wxid: str, content: str):
        """发送Cdn图片(转发图片)"""
        body = {
            "Wxid": wxid,
            "ToWxid": to_wxid,
            "Content": content
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    self._get_api_url("Msg/SendCDNImg"),
                    json=body
            ) as response:
                return await response.json()

    async def msg_send_cdn_video(self, wxid: str, to_wxid: str, content: str):
        """发送Cdn视频(转发视频)"""
        body = {
            "Wxid": wxid,
            "ToWxid": to_wxid,
            "Content": content
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    self._get_api_url("Msg/SendCDNVideo"),
                    json=body
            ) as response:
                return await response.json()

    async def msg_send_emoji(self, wxid: str, to_wxid: str, md5: str, total_len: int):
        """发送Emoji"""
        body = {
            "Wxid": wxid,
            "ToWxid": to_wxid,
            "Md5": md5,
            "TotalLen": total_len
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    self._get_api_url("Msg/SendEmoji"),
                    json=body
            ) as response:
                return await response.json()

    async def msg_send_txt(self, wxid: str, to_wxid: str, content: str, at_users: str = "", msg_type: int = 1):
        """发送文本消息"""
        body = {
            "Wxid": wxid,
            "ToWxid": to_wxid,
            "Content": content,
            "At": at_users,
            "Type": msg_type
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    self._get_api_url("Msg/SendTxt"),
                    json=body
            ) as response:
                return await response.json()

    async def msg_send_video(self, wxid: str, to_wxid: str, video_base64: str, image_base64: str, play_length: int):
        """发送视频"""
        body = {
            "Wxid": wxid,
            "ToWxid": to_wxid,
            "Base64": video_base64,
            "ImageBase64": image_base64,
            "PlayLength": play_length
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    self._get_api_url("Msg/SendVideo"),
                    json=body
            ) as response:
                return await response.json()

    async def msg_send_voice(self, wxid: str, to_wxid: str, voice_base64: str, voice_type: int, voice_time: int):
        """发送语音"""
        body = {
            "Wxid": wxid,
            "ToWxid": to_wxid,
            "Base64": voice_base64,
            "Type": voice_type,
            "VoiceTime": voice_time
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    self._get_api_url("Msg/SendVoice"),
                    json=body
            ) as response:
                return await response.json()

    async def msg_share_card(self, wxid: str, to_wxid: str, card_wx_id: str, card_nick_name: str, card_alias: str):
        """分享名片"""
        body = {
            "Wxid": wxid,
            "ToWxid": to_wxid,
            "CardWxId": card_wx_id,
            "CardNickName": card_nick_name,
            "CardAlias": card_alias
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    self._get_api_url("Msg/ShareCard"),
                    json=body
            ) as response:
                return await response.json()

    async def msg_share_link(self, wxid: str, to_wxid: str, link_type: int, desc: str, xml: str):
        """发送分享链接消息"""
        body = {
            "Wxid": wxid,
            "ToWxid": to_wxid,
            "Type": link_type,
            "Desc": desc,
            "Xml": xml
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Msg/ShareLink",
                    json=body
            ) as response:
                return await response.json()

    async def msg_share_location(self, wxid: str, to_wxid: str, x: float, y: float, scale: int, label: str, poiname: str, infourl: str):
        """分享位置"""
        body = {
            "Wxid": wxid,
            "ToWxid": to_wxid,
            "X": x,
            "Y": y,
            "Scale": scale,
            "Label": label,
            "Poiname": poiname,
            "Infourl": infourl
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Msg/ShareLocation",
                    json=body
            ) as response:
                return await response.json()

    async def msg_share_video(self, wxid: str, to_wxid: str, xml: str):
        """发送分享视频消息"""
        body = {
            "Wxid": wxid,
            "ToWxid": to_wxid,
            "Xml": xml
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Msg/ShareVideo",
                    json=body
            ) as response:
                return await response.json()
